package battleship.cxbr;

import java.util.ArrayList;

import battleship.emss.*;

public class RaceContext extends Context
{
	static final int lx=120;
	static final int ly=800;

	static final int rx=730;
	static final int ry=800;
	
	static final double tspeed=5.0;
	
	private int state;
	private int x,y;
	
	
	public RaceContext(String n,CxBRAgent a)
	{
		super(n,a);
	}
	
	//How to get here from above
	public double ParentRule(double Time)
	{
		return 0.0;
	}
	//How to get here from side
	public double SiblingRule(double Time)
	{
		return 0.0;
	}
	
	//Things to do when just entering
	public void Initialize(double Time)
	{
		state=0;
	}
	
	//What do actually do
	public void Action(double Time)
	{
		Agent.status.status=Name;
		Agent.actions.clear();
		
		//Basically racetrack, some run away
		if(state==0)
		{
			x=rx;
			y=ry;
		}
		else
		{
			x=lx;
			y=ly;
		}
		Agent.actions.add(new Move(Agent.status.x,Agent.status.y,x,y,tspeed));

		if(Math.sqrt(Math.pow(Agent.status.x-x,2)+Math.pow(Agent.status.y-y,2))<2)
		{
			//don't like the two state system
			state=1-state;
		}

		//Sends out if under attack
		//detected others
		for(int i=0;i<Agent.detectedStatus.size();i++)
		{
			Status tmpStatus=Agent.detectedStatus.get(i);
			//not on my team
			//And I am close enough
			//and it is alive
			
			if(tmpStatus.team!=Agent.status.team && tmpStatus.alive==1)
			{
				//less than 15 miles off
				if(Math.sqrt(Math.pow(Agent.status.mx-tmpStatus.mx,2)+Math.pow(Agent.status.my-tmpStatus.my,2))<20*1600)
				{
					Agent.status.status+=" Detect";
					Agent.outgoingMessages.add(new Message("You","Under Attack"));
				}
			}
		}		
		
		return;
	}
	
}

